//
//  LoginViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/22.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "LoginViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "RegisterViewController.h"
#import "ForgetPasswordViewController.h"
#import "ApplyForViewController.h"
#import "AuthorizeController.h"
#import "UMSocialQQHandler.h"
#import "UMSocialSnsPlatformManager.h"
#import "MainLineViewController.h"
#import "UMSocialAccountManager.h"
#import "UMSocialWechatHandler.h"
#import "AppDelegate.h"
#import "MainLineViewController.h"
#import "BindingViewController.h"
#import "AlertView.h"

#import <AdSupport/AdSupport.h>
#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <arpa/inet.h>


#import "LoginData.h"
#import "EncryptionData.h"
#import "JSONKit.h"
#import "GMDCircleLoader.h"

#import "Reachability.h"
#import "NetStatus.h"

@interface LoginViewController ()<UITextFieldDelegate,UIScrollViewDelegate,LoginDelegate>
{
    UIButton *weChatLoginBtn;
    UIScrollView *zScrollView;
    
    AlertView *_loginAlerView;
    UIView *_loginAlertBackview;
    UIView *zBack;
    UIView *alertView;
}
@property(nonatomic,retain)NSString *zUrl;
@end

@implementation LoginViewController
@synthesize zForgetPasswordBtn,zLoginNowBtn,zPassword,zPasswordStr,zPhoneNumber,zPhoneNumStr,zRegistrationBtn,zUrl;
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
   
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    self.navigationController.navigationBarHidden = YES;
    
    //[UMSocialWechatHandler setWXAppId:@"wxe42555fc43cf3c1d" appSecret:@"a2348ea607d68e3995472e1cd4d56012" url:@"http://www.yiwaku.com/api.php?hash=5ae14"];
    
    [self initUI];
    
    UITapGestureRecognizer *loginGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginGesturese)];
    [self.view addGestureRecognizer:loginGesture];
    
    NSString *UDIDStr=[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSString *systemStr= [[UIDevice currentDevice] systemVersion];
    [[NSUserDefaults standardUserDefaults]setObject:UDIDStr forKey:@"UDIDStr"];
    //NSLog(@"%@",UDIDStr);//唯一标识
    //NSLog(@"%@",systemStr);//ios版本

    LoginData *data = [[LoginData alloc]init];
    data.delegate = self;
    [data version:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] andDeviceType:@"2"];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}
#pragma mark - 强制更新
-(void)createYue:(NSString *)str{
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate] window];
    
    zBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    zBack.backgroundColor = [UIColor blackColor];
    zBack.alpha = 0.4;
    [windows addSubview:zBack];
    
    alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-70, SCREEN_WIDTH-120, 140)];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled = YES;
    alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
    [windows addSubview:alertView];
    
    UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, alertView.frame.size.width-70, 40)];
    alertLabel.text = [NSString stringWithFormat:@"您的版本过旧，请更新至%@版本",str];
    alertLabel.numberOfLines = 0;
    alertLabel.font = [UIFont systemFontOfSize:14];
    alertLabel.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:alertLabel];
    
    UIView *lines = [[UIView alloc] initWithFrame:CGRectMake(0, 140-29, alertView.frame.size.width, 0.5)];
    lines.backgroundColor = [UIColor grayColor];
    [alertView addSubview:lines];
  
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 140-28, alertView.frame.size.width, 28);
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = leftBtn.bounds;
    maskLayer1.path = maskPath1.CGPath;
    leftBtn.layer.mask = maskLayer1;
    
    [leftBtn addTarget:self action:@selector(clickUpdate) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateHighlighted];
    [alertView addSubview:leftBtn];
    
}
//非强制性的
-(void)createYue1:(NSString *)str{
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate] window];
    
    zBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    zBack.backgroundColor = [UIColor blackColor];
    zBack.alpha = 0.4;
    [windows addSubview:zBack];
    
    alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-70, SCREEN_WIDTH-120, 140)];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled = YES;
    alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
    [windows addSubview:alertView];
    
    UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, alertView.frame.size.width-40, 40)];
    alertLabel.text = [NSString stringWithFormat:@"检测到新版本，建议更新至%@版本",str];
    alertLabel.numberOfLines = 0;
    alertLabel.font = [UIFont systemFontOfSize:14];
    alertLabel.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:alertLabel];

    UIView *lines = [[UIView alloc] initWithFrame:CGRectMake(0, 140-29, alertView.frame.size.width, 0.5)];
    lines.backgroundColor = [UIColor grayColor];
    [alertView addSubview:lines];

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 140-28, alertView.frame.size.width/2, 28);
    //leftBtn.backgroundColor= [ UIColor whiteColor];
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = leftBtn.bounds;
    maskLayer1.path = maskPath1.CGPath;
    leftBtn.layer.mask = maskLayer1;
    [leftBtn addTarget:self action:@selector(clickUpdate) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateHighlighted];
    [alertView addSubview:leftBtn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(alertView.frame.size.width/2, 140-28, alertView.frame.size.width/2, 28);
    UIBezierPath *maskPath11 = [UIBezierPath bezierPathWithRoundedRect:rightBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer11 = [[CAShapeLayer alloc] init];
    maskLayer11.frame = rightBtn.bounds;
    maskLayer11.path = maskPath11.CGPath;
    rightBtn.layer.mask = maskLayer11;
    [rightBtn addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateHighlighted];
    [alertView addSubview:rightBtn];
    
}
#pragma mark - 版本检测回调
-(void)version:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    if ([[NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"IsLatestVersion"]]isEqualToString:@"2"]) {
        zUrl = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"DownloadUrl"]];
        if ([[[sign objectForKey:@"data"] objectForKey:@"IsForceUpdate"]isEqualToString:@"1"]) {
            [self createYue:[NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"Version"]]]; //强制性的
        }else{
            [self createYue1:[NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"Version"]]];//非强制性的
        }
    }
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 确定更新
-(void)clickUpdate{
    NSURL* url = [[ NSURL alloc ] initWithString :zUrl];
    [[UIApplication sharedApplication ] openURL: url];
}
#pragma mark - 非强制性的取消更新
-(void)clickCancel{
    [zBack removeFromSuperview];
    zBack = nil;
    [alertView removeFromSuperview];
    alertView = nil;
}
- (NSString *)getIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    // Free memory
    freeifaddrs(interfaces);
    
    return address;
}

-(void)loginGesturese{
    [zPhoneNumber resignFirstResponder];
    [zPassword resignFirstResponder];
    if (SCREEN_HEIGHT<500) {
    
        zScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT*1.15);
    }
}
#pragma mark - initUI
-(void)initUI{
    zScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    zScrollView.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    if (SCREEN_HEIGHT<500) {
        zScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+50);
    }else{
        zScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-20);
    }
    zScrollView.userInteractionEnabled = YES;
    [self.view addSubview:zScrollView];
    
    UIImageView *topLogo = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-SCREEN_WIDTH*0.77)/2,60, SCREEN_WIDTH*0.77, SCREEN_WIDTH*0.459)];
    topLogo.image = [UIImage imageNamed:@"登录_03"];
    topLogo.userInteractionEnabled = YES;
    [zScrollView addSubview:topLogo];
    
    
    UIImageView *loginPwdBG = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-SCREEN_WIDTH*0.875)/2, topLogo.bottom+60, SCREEN_WIDTH*0.875, SCREEN_WIDTH*0.25)];
    loginPwdBG.image = [UIImage imageNamed:@"kuang"];
    //loginPwdBG.backgroundColor = [UIColor whiteColor];
    loginPwdBG.userInteractionEnabled = YES;
    [zScrollView addSubview:loginPwdBG];
    
    zPhoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,loginPwdBG.frame.size.width-100 , loginPwdBG.frame.size.height/2)];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"iPhone"];
    if (str.length < 1) {
        zPhoneNumber.placeholder = @"手机号";
    }else{
        zPhoneNumber.text = str;
    }
    zPhoneNumber.font = [UIFont systemFontOfSize:14];
    zPhoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    [loginPwdBG addSubview:zPhoneNumber];
    zPhoneNumber.delegate = self;
    
    
    zPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, loginPwdBG.frame.size.height/2,loginPwdBG.frame.size.width-100,loginPwdBG.frame.size.height/2)];
    zPassword.placeholder = @"密码";
    zPassword.delegate = self;
    zPassword.font = [UIFont systemFontOfSize:14];
    zPassword.secureTextEntry = YES;
    [loginPwdBG addSubview:zPassword];
    
    
    
    zForgetPasswordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    zForgetPasswordBtn.frame = CGRectMake(0.0625*SCREEN_WIDTH, loginPwdBG.bottom+10, 60, 20);
    [zForgetPasswordBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [zForgetPasswordBtn addTarget:self action:@selector(clickForgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [zForgetPasswordBtn setTitleColor:[self colorWithHexString:@"#54A7DE"] forState:UIControlStateNormal];
    zForgetPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [zScrollView addSubview:zForgetPasswordBtn];
    
    zRegistrationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    zRegistrationBtn.frame = CGRectMake(SCREEN_WIDTH-0.0625*SCREEN_WIDTH-26, loginPwdBG.bottom+10, 26, 20);
    [zRegistrationBtn setTitle:@"注册" forState:UIControlStateNormal];
    [zRegistrationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [zRegistrationBtn addTarget:self action:@selector(clickRegostration) forControlEvents:UIControlEventTouchUpInside];
    zRegistrationBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    //[self.view addSubview:zRegistrationBtn];
    
    zLoginNowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    zLoginNowBtn.frame = CGRectMake((SCREEN_WIDTH-SCREEN_WIDTH*0.875)/2, zRegistrationBtn.bottom+40, SCREEN_WIDTH*0.875, SCREEN_WIDTH*0.125);
    [zLoginNowBtn setBackgroundImage:[UIImage imageNamed:@"login"] forState:UIControlStateNormal];
    [zLoginNowBtn setBackgroundImage:[UIImage imageNamed:@"login1"] forState:UIControlStateHighlighted];
    [zLoginNowBtn addTarget:self action:@selector(clickLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    [zScrollView addSubview:zLoginNowBtn];
    
    
    weChatLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    weChatLoginBtn.frame = CGRectMake((SCREEN_WIDTH-SCREEN_WIDTH*0.875)/2, zLoginNowBtn.bottom+20, SCREEN_WIDTH*0.875, SCREEN_WIDTH*0.125);
    [weChatLoginBtn setBackgroundImage:[UIImage imageNamed:@"denglu_weixin"] forState:UIControlStateNormal];
    [weChatLoginBtn addTarget:self action:@selector(clickWeChatBtn) forControlEvents:UIControlEventTouchUpInside];
    [zScrollView addSubview:weChatLoginBtn];
    
    
    
    //    UIImageView *speedLogin = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"action_04"]];
    //    speedLogin.frame = CGRectMake(0, zLoginNowBtn.bottom+70, SCREEN_WIDTH, 14);
    //    [self.view addSubview:speedLogin];
    //
    //
    //    UIButton *zWeChatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    zWeChatBtn.frame = CGRectMake(115, speedLogin.bottom+20, 90, 90);
    //    [zWeChatBtn setBackgroundImage:[self circleImage:[UIImage imageNamed:@"action_08"] withParam:0] forState:UIControlStateNormal];
    //    [zWeChatBtn addTarget:self action:@selector(clickWeChatBtn) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:zWeChatBtn];
    
}

#pragma -mark 创建提示框
- (void)createAlertView{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    _loginAlertBackview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _loginAlertBackview.backgroundColor = [UIColor blackColor];
    _loginAlertBackview.alpha = 0.4;
    [window addSubview:_loginAlertBackview];
    _loginAlerView = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_mall_xibs" owner:self options:nil] objectAtIndex:10];
    _loginAlerView.frame = CGRectMake(50, SCREEN_HEIGHT/2.0-130/2.0, SCREEN_WIDTH-100, 130);
    [_loginAlerView.confirmBut addTarget:self action:@selector(confirmButClick) forControlEvents:UIControlEventTouchUpInside];
    [_loginAlerView.cancelBut addTarget:self action:@selector(cancelButClick) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:_loginAlerView];
}
#pragma -mark 点击提示框去绑定银行卡事件
- (void)confirmButClick{
    [_loginAlerView removeFromSuperview];
    [_loginAlertBackview removeFromSuperview];
    BindingViewController *bindingVC = [[BindingViewController alloc] init];
    bindingVC.controller = self;
    [self.navigationController pushViewController:bindingVC animated:YES];
}

#pragma -mark 点击提示框稍后按钮
- (void)cancelButClick{
    [_loginAlerView removeFromSuperview];
    [_loginAlertBackview removeFromSuperview];
    
}

#pragma mark - 忘记密码
-(void)clickForgetPassword{
    ForgetPasswordViewController *forget = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forget animated:YES];
}
#pragma mark - 注册
-(void)clickRegostration{
    ApplyForViewController *becomMerchant = [[ApplyForViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:becomMerchant animated:YES];
}
#pragma mark - 登录
-(void)clickLoginBtn:(UIButton *)sender{
    
    //先将未到时间执行前的任务取消。
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender];
    [self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f];
    
}
- (void)todoSomething:(id)sender{

    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (zPhoneNumber.text.length <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入登录账号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (zPassword.text.length <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    LoginData *login = [[LoginData alloc]init];
    login.delegate = self;
    //[login mainUrl:nil];
    NSString *udidStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"UDIDStr"];
    NSLog(@"%@",[self getIPAddress]);
    [login loginMobileNumber:zPhoneNumber.text andLoginPass:zPassword.text andDeviceType:@"2" andDeviceNumber:udidStr andLastLoginIP:[self getIPAddress]];
    [GMDCircleLoader setOnView:self.view withTitle:@"登录中..." animated:YES];
    //    [self createAlertView];
}
#pragma mark - UItextField Delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    zScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+100);
    [zScrollView setContentOffset:CGPointMake(0, 100) animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    zScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-20);
    return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    zScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-20);
}
#pragma mark - login delegate
-(void)login:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
        [[NSUserDefaults standardUserDefaults]setObject:zPhoneNumber.text forKey:@"iPhone"];
        NSLog(@"%@",[[sign objectForKey:@"data"] objectForKey:@"AgentID"]);
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"AgentID"] forKey:@"userID"];
        if ([[[sign objectForKey:@"data"] objectForKey:@"FatherAgent"] isEqual:[NSNull null]] || [[[sign objectForKey:@"data"] objectForKey:@"FatherLevelID"] isEqual:[NSNull null]] || [[[sign objectForKey:@"data"] objectForKey:@"FatherLevelName"] isEqual:[NSNull null]]){
            
        }else{
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherAgent"] forKey:@"FatherAgent"];
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherLevelID"] forKey:@"FatherLevelID"];
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherLevelName"] forKey:@"FatherLevelName"];
        }
        NSString *isbind = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"IsBindBankcard"]];
        [[NSUserDefaults standardUserDefaults]setObject:isbind forKey:@"IsBindBankcard"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"LevelID"] forKey:@"LevelID"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"LevelName"] forKey:@"LevelName"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"RealName"] forKey:@"RealName"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"MobileNumber"] forKey:@"MobileNumber"];
        NSString *ispayoffdeposit = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"IsPayOffDeposit"]];
        [[NSUserDefaults standardUserDefaults]setObject:ispayoffdeposit forKey:@"IsPayOffDeposit"];

        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"shifouxianshi"];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        MainLineViewController *mainBar = storyboard.instantiateInitialViewController;
        [[[UIApplication sharedApplication] delegate] window] .rootViewController =mainBar;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - loginError
-(void)requistFail{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - 微信登录
-(void)clickWeChatBtn{
    //    AuthorizeController *authorize = [[AuthorizeController alloc]init];
    //    [self.navigationController pushViewController:authorize animated:YES];
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
             [[NSUserDefaults standardUserDefaults]setObject:snsAccount.usid forKey:@"openID"];
            LoginData *data  =[[LoginData alloc]init];
            data.delegate = self;
            [data checkwx:snsAccount.usid];
            //userID暂时
            //[[NSUserDefaults standardUserDefaults]setObject:@"27" forKey:@"userID"];

        }
        
    });
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToWechatSession  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
    }];
    
}
#pragma mark - 微信验证
-(void)checkwx:(NSDictionary *)sign{
    NSLog(@"%@",sign);

    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"AgentID"] forKey:@"userID"];
        if ([[[sign objectForKey:@"data"] objectForKey:@"FatherAgent"] isEqual:[NSNull null]] || [[[sign objectForKey:@"data"] objectForKey:@"FatherLevelID"] isEqual:[NSNull null]] || [[[sign objectForKey:@"data"] objectForKey:@"FatherLevelName"] isEqual:[NSNull null]]){
            
        }else{
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherAgent"] forKey:@"FatherAgent"];
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherLevelID"] forKey:@"FatherLevelID"];
            [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"FatherLevelName"] forKey:@"FatherLevelName"];
        }
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"IsBindBankcard"] forKey:@"IsBindBankcard"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"LevelID"] forKey:@"LevelID"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"RealName"] forKey:@"RealName"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"LevelName"] forKey:@"LevelName"];

        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"MobileNumber"] forKey:@"MobileNumber"];
        [[NSUserDefaults standardUserDefaults]setObject:[[sign objectForKey:@"data"] objectForKey:@"IsPayOffDeposit"] forKey:@"IsPayOffDeposit"];
        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"shifouxianshi"];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        MainLineViewController *mainBar = storyboard.instantiateInitialViewController;
        [[[UIApplication sharedApplication] delegate] window] .rootViewController =mainBar;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }else{
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"shifouxianshi"];
         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         MainLineViewController *mainBar = storyboard.instantiateInitialViewController;
         [[[UIApplication sharedApplication] delegate] window] .rootViewController =mainBar;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
